package com.googlecode.maven.plugin.appassembler;

import com.googlecode.maven.plugin.appassembler.launcher.jsw.JswLauncher;
import com.googlecode.maven.plugin.appassembler.launcher.jsw.JswLauncherConfiguration;
import com.googlecode.maven.plugin.appassembler.launcher.program.ProgramLauncher;
import com.googlecode.maven.plugin.appassembler.launcher.program.ProgramLauncherConfiguration;
import java.io.File;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Generates launcher files.
 */
public final class LauncherManager
    {
    /**
     * Constructs a new LauncherManager object.
     */
    private LauncherManager()
        {
        super();
        }

    /**
     * Generates launcher files.
     *
     * @param configuration Launcher configuration.
     * @param app Application.
     * @param targetDirectory Target directory.
     * @throws MojoFailureException If this operation failed.
     */
    public static void apply(LauncherConfiguration launcherConfiguration,
            App app, File targetDirectory)
            throws MojoFailureException
        {
        if (launcherConfiguration == null)
            {
            return;
            }
        boolean launcherConfigured = false;
        if (launcherConfiguration.getJsw() != null)
            {
            if (launcherConfigured)
                {
                throw new MojoFailureException("Multiple launchers not allowed.");
                }
            JswLauncherConfiguration jswLauncherConfiguration = launcherConfiguration.
                    getJsw();
            JswLauncher.apply(jswLauncherConfiguration, app, targetDirectory);
            launcherConfigured = true;
            }
        if (launcherConfiguration.getProgram() != null)
            {
            if (launcherConfigured)
                {
                throw new MojoFailureException("Multiple launchers not allowed.");
                }
            ProgramLauncherConfiguration programLauncherConfiguration = launcherConfiguration.
                    getProgram();
            ProgramLauncher.apply(programLauncherConfiguration, app,
                    targetDirectory);
            launcherConfigured = true;
            }
        }
    }
